package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.OrderProduct;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.OrderProductService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: xsc_hunter
 * @Date: 2024/12/4) 下午3:13
 * @Version: 1.0
 */
@RestController
@RequestMapping("/orderProduct")
@AllArgsConstructor
@ResponseResult
public class OrderProductController {

    private final OrderProductService orderProductService;

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String order_id = req.getParameter("order_id");
        String product_id = req.getParameter("product_id");
        Integer product_num = Integer.valueOf(req.getParameter("product_num"));

        Boolean save = orderProductService.save(new OrderProduct(id, order_id, product_id,product_num));
       return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String order_id = req.getParameter("order_id");
        String product_id = req.getParameter("product_id");
        Integer product_num = Integer.valueOf(req.getParameter("product_num"));

        Boolean update = orderProductService.update(new OrderProduct(id, order_id, product_id, product_num));
        return ResponseVo.buildSuccess(update);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        boolean rs = orderProductService.remove(id);
        return ResponseVo.buildSuccess(rs);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        OrderProduct orderProduct = orderProductService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(orderProduct);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderProduct> orderProducts = orderProductService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(orderProducts);
    }
}
